﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace SCResources.General
{
    public class SerieWrapper :IComparable
    {
        private string title;
        private int dbId;
        private string imdbId;
        //private MemoryStream cover;
        private Image cover;
        private string coverPath;
        private string localDirectory;
        private bool autoAirInfo;

        private SerieUserInfo userInfo;
        private SerieAirInfo airInfo;

        #region Properties

        public int DbId
        {
            get
            {
                return dbId;
            }
            set
            {
                dbId = value;
            }
        }

        public string ImdbId
        {
            get
            {
                return imdbId;
            }
            set
            {
                imdbId = value;
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        public bool AutoAirInfo
        {
            get
            {
                return autoAirInfo;
            }
            set
            {
                autoAirInfo = value;
            }
        }

        public Image Cover
        {
            get
            {
                return cover;
            }
            set
            {
                cover = value;
            }
        }

        public string CoverPath
        {
            get
            {
                return coverPath;
            }
            set
            {
                coverPath = value;
            }
        }

        public string LocalDirectory
        {
            get
            {
                return localDirectory;
            }
            set
            {
                localDirectory = value;
            }
        }

        public SerieUserInfo UserInfo
        {
            get
            {
                return userInfo;
            }
            set
            {
                userInfo = value;
            }
        }

        public SerieAirInfo AirInfo
        {
            get
            {
                return airInfo;
            }
            set
            {
                airInfo = value;
            }
        }

        #endregion


        public SerieWrapper()
        {
        }

        
        public SerieWrapper(string title,int id, string imdbId, string coverPath, string localDirectory, bool autoAirInfo)
        {
            this.title = title;
            this.dbId = id;
            this.imdbId = imdbId;
            this.coverPath = coverPath;
            this.localDirectory = localDirectory;
            this.autoAirInfo = autoAirInfo;
        }

        public SerieWrapper(string title, string imdbId, Image cover, string localDirectory, SerieUserInfo userInfo)
        {
            this.title = title;
            this.imdbId = imdbId;
            this.cover = cover;
            this.localDirectory = localDirectory;
            this.userInfo = userInfo;
            autoAirInfo = true;
        }

        public SerieWrapper(string title, string imdbId, Image cover, string localDirectory, SerieUserInfo userInfo, SerieAirInfo airInfo)
        {
            this.title = title;
            this.imdbId = imdbId;
            this.cover = cover;
            this.localDirectory = localDirectory;
            this.userInfo = userInfo;
            this.airInfo = airInfo;
            autoAirInfo = true;
        }

        /// <summary>
        /// Compare To
        /// </summary>
        /// <param name="obj">Object to compare to</param>
        /// <returns>
        /// 0: Base object isn't equal
        /// 1: Base object is equal, airinfo & userinfo arn't equal
        /// 2: Base & userinfo are equeal
        /// 3: Base & airInfo are equal
        /// 4: All is equal
        /// +10: userinfo is null
        /// +100: airinfo is null
        /// </returns>
        public int CompareTo(object obj)
        {
            SerieWrapper temp = (SerieWrapper) obj;

            if (this.title != temp.title ||
                this.cover != temp.cover ||
                this.coverPath != temp.coverPath ||
                this.localDirectory != temp.localDirectory)
            {
                return 0;
            }
            else
            {
                int sum = 1;
                if (userInfo != null)
                {
                    sum += userInfo.CompareTo(temp.userInfo);
                }
                else
                {
                    sum += 10;
                }
                if(airInfo != null)
                {
                    sum += airInfo.CompareTo(temp.airInfo);
                }
                else
                {
                    sum += 100;
                }

                return sum;
            }
        }
    }



    public class SerieUserInfo : IComparable
    {
        private int season;
        private int episode;

        #region Properties

        public int Season
        {
            get
            {
                return season;
            }
            set
            {
                season = value;
            }
        }

        public int Episode
        {
            get
            {
                return episode;
            }
            set
            {
                episode = value;
            }
        }

        #endregion


        public SerieUserInfo(int season, int episode)
        {
            this.season = season;
            this.episode = episode;
        }

        /// <summary>
        /// Compare To
        /// </summary>
        /// <param name="obj">Object to compare to</param>
        /// <returns>
        /// 0: Not equal
        /// 1: All equal
        /// </returns>
        public int CompareTo(object obj)
        {
            SerieUserInfo temp = (SerieUserInfo) obj;
            
            if(this.season != temp.season || this.episode != temp.episode)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }

    public class SerieAirInfo : IComparable
    {
        private int season;
        private int episode;
        private DateTime nextAirDate;
        private int frequency;

        #region Properties

        public int Season
        {
            get
            {
                return season;
            }
            set
            {
                season = value;
            }
        }

        public int Episode
        {
            get
            {
                return episode;
            }
            set
            {
                episode = value;
            }
        }

        public DateTime NextAirDate
        {
            get
            {
                return nextAirDate;
            }
            set
            {
                nextAirDate = value;
            }
        }

        public int Frequency
        {
            get
            {
                return frequency;
            }
            set
            {
                frequency = value;
            }
        }

        #endregion

        public SerieAirInfo(){}

        public SerieAirInfo(int season, int episode, DateTime nextAirDate, int frequency = 0)
        {
            this.season = season;
            this.episode = episode;
            this.nextAirDate = nextAirDate;
            this.frequency = frequency;
        }

        /// <summary>
        /// Compare To
        /// </summary>
        /// <param name="obj">Object to compare to</param>
        /// <returns>
        /// 0: Not equal
        /// 2: All equal
        /// </returns>
        public int CompareTo(object obj)
        {
            SerieAirInfo temp = (SerieAirInfo) obj;

            if (this.season != temp.season ||
                this.episode != temp.episode ||
                this.nextAirDate != temp.nextAirDate ||
                this.frequency != temp.frequency)
            {
                return 0;
            }
            else
            {
                return 2;
            }
        }
    }
}
